package com.seamtop.cuber.web.auth;

import com.alibaba.fastjson.JSONObject;
import com.seamtop.cuber.common.entriy.CuberContants;
import com.seamtop.cuber.common.util.EncryptUtil;
import com.seamtop.cuber.common.util.MD5Util;
import com.seamtop.cuber.common.entriy.ESResult;
import com.seamtop.cuber.common.entriy.ESResultItem;
import com.seamtop.cuber.common.entriy.User;
import com.seamtop.cuber.web.service.UserManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zongjunfeng on 2015/9/16.
 */
@Controller("LoginController")
public class LoginController {

    private static final Logger LOG  = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserManagerService userManagerService;

    @RequestMapping("/login/index.shtml")
    public String loginIndex(HttpServletRequest request, HttpServletResponse response,
                        ModelMap model) {
        System.out.println("login -- index");
        return "/login/login_index";
    }

    @RequestMapping("/loginOut.shtml")
    public @ResponseBody
    JSONObject loginOut(HttpServletRequest request, HttpServletResponse response,
                     ModelMap model) {
        JSONObject object = new JSONObject();
        request.getSession().removeAttribute("loginedUser");
        object.put("result", "success");
        try{
            response.sendRedirect("/login/index.shtml");
        }catch (Exception e){
            e.printStackTrace();
        }
        return object;
    }

    @RequestMapping("/login.shtml")
    public @ResponseBody
    JSONObject login(HttpServletRequest request, HttpServletResponse response,
                       ModelMap model) {
        JSONObject object = new JSONObject();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        try{
            userName = EncryptUtil.desEncrypt(userName, CuberContants.SERVER_ENCRYPT_KEY);
            password = EncryptUtil.desEncrypt(password, CuberContants.SERVER_ENCRYPT_KEY);
            User user = new User();
            user.setUserName(userName);
            user.setPassword(password);
            int i = userManagerService.isUserExist(user);
            if(i > 0){
                LOG.info("User[name:"+userName+",password:"+password+"] login in success!");
                request.getSession().setAttribute("loginedUser",user);
                object.put("result","success");
            }else{
                LOG.info("User[name:"+userName+",password:"+password+"] login in failure!");
                object.put("result","fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.put("result","fail");
        }
        return object;
    }

    public static void main(String [] args){
        String s = MD5Util.getMD5Code("12345");
        System.out.println(s);
    }
}
